1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gio.TlsConnection; 26 27 private import gio.AsyncResultIF; 28 private import gio.Cancellable; 29 private import gio.IOStream; 30 private import gio.TlsCertificate; 31 private import gio.TlsDatabase; 32 private import gio.TlsInteraction; 33 private import gio.c.functions; 34 public import gio.c.types; 35 private import glib.ByteArray; 36 private import glib.ErrorG; 37 private import glib.GException; 38 private import glib.MemorySlice; 39 private import glib.Str; 40 private import glib.c.functions; 41 private import gobject.ObjectG; 42 private import gobject.Signals; 43 private import std.algorithm; 44 45 46 /** 47 * #GTlsConnection is the base TLS connection class type, which wraps 48 * a #GIOStream and provides TLS encryption on top of it. Its 49 * subclasses, #GTlsClientConnection and #GTlsServerConnection, 50 * implement client-side and server-side TLS, respectively. 51 * 52 * For DTLS (Datagram TLS) support, see #GDtlsConnection. 53 * 54 * Since: 2.28 55 */ 56 public class TlsConnection : IOStream 57 { 58 /** the main Gtk struct */ 59 protected GTlsConnection* gTlsConnection; 60 61 /** Get the main Gtk struct */ 62 public GTlsConnection* getTlsConnectionStruct(bool transferOwnership = false) 63 { 64 if (transferOwnership) 65 ownedRef = false; 66 return gTlsConnection; 67 } 68 69 /** the main Gtk struct as a void* */ 70 protected override void* getStruct() 71 { 72 return cast(void*)gTlsConnection; 73 } 74 75 /** 76 * Sets our main struct and passes it to the parent class. 77 */ 78 public this (GTlsConnection* gTlsConnection, bool ownedRef = false) 79 { 80 this.gTlsConnection = gTlsConnection; 81 super(cast(GIOStream*)gTlsConnection, ownedRef); 82 } 83 84 85 /** */ 86 public static GType getType() 87 { 88 return g_tls_connection_get_type(); 89 } 90 91 /** 92 * Used by #GTlsConnection implementations to emit the 93 * #GTlsConnection::accept-certificate signal. 94 * 95 * Params: 96 * peerCert = the peer's #GTlsCertificate 97 * errors = the problems with @peer_cert 98 * 99 * Returns: %TRUE if one of the signal handlers has returned 100 * %TRUE to accept @peer_cert 101 * 102 * Since: 2.28 103 */ 104 public bool emitAcceptCertificate(TlsCertificate peerCert, GTlsCertificateFlags errors) 105 { 106 return g_tls_connection_emit_accept_certificate(gTlsConnection, (peerCert is null) ? null : peerCert.getTlsCertificateStruct(), errors) != 0; 107 } 108 109 /** 110 * Gets @conn's certificate, as set by 111 * g_tls_connection_set_certificate(). 112 * 113 * Returns: @conn's certificate, or %NULL 114 * 115 * Since: 2.28 116 */ 117 public TlsCertificate getCertificate() 118 { 119 auto __p = g_tls_connection_get_certificate(gTlsConnection); 120 121 if(__p is null) 122 { 123 return null; 124 } 125 126 return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) __p); 127 } 128 129 /** 130 * Query the TLS backend for TLS channel binding data of @type for @conn. 131 * 132 * This call retrieves TLS channel binding data as specified in RFC 133 * [5056](https://tools.ietf.org/html/rfc5056), RFC 134 * [5929](https://tools.ietf.org/html/rfc5929), and related RFCs. The 135 * binding data is returned in @data. The @data is resized by the callee 136 * using #GByteArray buffer management and will be freed when the @data 137 * is destroyed by g_byte_array_unref(). If @data is %NULL, it will only 138 * check whether TLS backend is able to fetch the data (e.g. whether @type 139 * is supported by the TLS backend). It does not guarantee that the data 140 * will be available though. That could happen if TLS connection does not 141 * support @type or the binding data is not available yet due to additional 142 * negotiation or input required. 143 * 144 * Params: 145 * type = #GTlsChannelBindingType type of data to fetch 146 * data = #GByteArray is 147 * filled with the binding data, or %NULL 148 * 149 * Returns: %TRUE on success, %FALSE otherwise 150 * 151 * Since: 2.66 152 * 153 * Throws: GException on failure. 154 */ 155 public bool getChannelBindingData(GTlsChannelBindingType type, out ByteArray data) 156 { 157 GByteArray* outdata = sliceNew!GByteArray(); 158 GError* err = null; 159 160 auto __p = g_tls_connection_get_channel_binding_data(gTlsConnection, type, outdata, &err) != 0; 161 162 if (err !is null) 163 { 164 throw new GException( new ErrorG(err) ); 165 } 166 167 data = new ByteArray(outdata, true); 168 169 return __p; 170 } 171 172 /** 173 * Returns the name of the current TLS ciphersuite, or %NULL if the 174 * connection has not handshaked or has been closed. Beware that the TLS 175 * backend may use any of multiple different naming conventions, because 176 * OpenSSL and GnuTLS have their own ciphersuite naming conventions that 177 * are different from each other and different from the standard, IANA- 178 * registered ciphersuite names. The ciphersuite name is intended to be 179 * displayed to the user for informative purposes only, and parsing it 180 * is not recommended. 181 * 182 * Returns: The name of the current TLS ciphersuite, or %NULL 183 * 184 * Since: 2.70 185 */ 186 public string getCiphersuiteName() 187 { 188 auto retStr = g_tls_connection_get_ciphersuite_name(gTlsConnection); 189 190 scope(exit) Str.freeString(retStr); 191 return Str.toString(retStr); 192 } 193 194 /** 195 * Gets the certificate database that @conn uses to verify 196 * peer certificates. See g_tls_connection_set_database(). 197 * 198 * Returns: the certificate database that @conn uses or %NULL 199 * 200 * Since: 2.30 201 */ 202 public TlsDatabase getDatabase() 203 { 204 auto __p = g_tls_connection_get_database(gTlsConnection); 205 206 if(__p is null) 207 { 208 return null; 209 } 210 211 return ObjectG.getDObject!(TlsDatabase)(cast(GTlsDatabase*) __p); 212 } 213 214 /** 215 * Get the object that will be used to interact with the user. It will be used 216 * for things like prompting the user for passwords. If %NULL is returned, then 217 * no user interaction will occur for this connection. 218 * 219 * Returns: The interaction object. 220 * 221 * Since: 2.30 222 */ 223 public TlsInteraction getInteraction() 224 { 225 auto __p = g_tls_connection_get_interaction(gTlsConnection); 226 227 if(__p is null) 228 { 229 return null; 230 } 231 232 return ObjectG.getDObject!(TlsInteraction)(cast(GTlsInteraction*) __p); 233 } 234 235 /** 236 * Gets the name of the application-layer protocol negotiated during 237 * the handshake. 238 * 239 * If the peer did not use the ALPN extension, or did not advertise a 240 * protocol that matched one of @conn's protocols, or the TLS backend 241 * does not support ALPN, then this will be %NULL. See 242 * g_tls_connection_set_advertised_protocols(). 243 * 244 * Returns: the negotiated protocol, or %NULL 245 * 246 * Since: 2.60 247 */ 248 public string getNegotiatedProtocol() 249 { 250 return Str.toString(g_tls_connection_get_negotiated_protocol(gTlsConnection)); 251 } 252 253 /** 254 * Gets @conn's peer's certificate after the handshake has completed 255 * or failed. (It is not set during the emission of 256 * #GTlsConnection::accept-certificate.) 257 * 258 * Returns: @conn's peer's certificate, or %NULL 259 * 260 * Since: 2.28 261 */ 262 public TlsCertificate getPeerCertificate() 263 { 264 auto __p = g_tls_connection_get_peer_certificate(gTlsConnection); 265 266 if(__p is null) 267 { 268 return null; 269 } 270 271 return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) __p); 272 } 273 274 /** 275 * Gets the errors associated with validating @conn's peer's 276 * certificate, after the handshake has completed or failed. (It is 277 * not set during the emission of #GTlsConnection::accept-certificate.) 278 * 279 * See #GTlsConnection:peer-certificate-errors for more information. 280 * 281 * Returns: @conn's peer's certificate errors 282 * 283 * Since: 2.28 284 */ 285 public GTlsCertificateFlags getPeerCertificateErrors() 286 { 287 return g_tls_connection_get_peer_certificate_errors(gTlsConnection); 288 } 289 290 /** 291 * Returns the current TLS protocol version, which may be 292 * %G_TLS_PROTOCOL_VERSION_UNKNOWN if the connection has not handshaked, or 293 * has been closed, or if the TLS backend has implemented a protocol version 294 * that is not a recognized #GTlsProtocolVersion. 295 * 296 * Returns: The current TLS protocol version 297 * 298 * Since: 2.70 299 */ 300 public GTlsProtocolVersion getProtocolVersion() 301 { 302 return g_tls_connection_get_protocol_version(gTlsConnection); 303 } 304 305 /** 306 * Gets @conn rehandshaking mode. See 307 * g_tls_connection_set_rehandshake_mode() for details. 308 * 309 * Deprecated: Changing the rehandshake mode is no longer 310 * required for compatibility. Also, rehandshaking has been removed 311 * from the TLS protocol in TLS 1.3. 312 * 313 * Returns: %G_TLS_REHANDSHAKE_SAFELY 314 * 315 * Since: 2.28 316 */ 317 public GTlsRehandshakeMode getRehandshakeMode() 318 { 319 return g_tls_connection_get_rehandshake_mode(gTlsConnection); 320 } 321 322 /** 323 * Tests whether or not @conn expects a proper TLS close notification 324 * when the connection is closed. See 325 * g_tls_connection_set_require_close_notify() for details. 326 * 327 * Returns: %TRUE if @conn requires a proper TLS close 328 * notification. 329 * 330 * Since: 2.28 331 */ 332 public bool getRequireCloseNotify() 333 { 334 return g_tls_connection_get_require_close_notify(gTlsConnection) != 0; 335 } 336 337 /** 338 * Gets whether @conn uses the system certificate database to verify 339 * peer certificates. See g_tls_connection_set_use_system_certdb(). 340 * 341 * Deprecated: Use g_tls_connection_get_database() instead 342 * 343 * Returns: whether @conn uses the system certificate database 344 */ 345 public bool getUseSystemCertdb() 346 { 347 return g_tls_connection_get_use_system_certdb(gTlsConnection) != 0; 348 } 349 350 /** 351 * Attempts a TLS handshake on @conn. 352 * 353 * On the client side, it is never necessary to call this method; 354 * although the connection needs to perform a handshake after 355 * connecting (or after sending a "STARTTLS"-type command), 356 * #GTlsConnection will handle this for you automatically when you try 357 * to send or receive data on the connection. You can call 358 * g_tls_connection_handshake() manually if you want to know whether 359 * the initial handshake succeeded or failed (as opposed to just 360 * immediately trying to use @conn to read or write, in which case, 361 * if it fails, it may not be possible to tell if it failed before or 362 * after completing the handshake), but beware that servers may reject 363 * client authentication after the handshake has completed, so a 364 * successful handshake does not indicate the connection will be usable. 365 * 366 * Likewise, on the server side, although a handshake is necessary at 367 * the beginning of the communication, you do not need to call this 368 * function explicitly unless you want clearer error reporting. 369 * 370 * Previously, calling g_tls_connection_handshake() after the initial 371 * handshake would trigger a rehandshake; however, this usage was 372 * deprecated in GLib 2.60 because rehandshaking was removed from the 373 * TLS protocol in TLS 1.3. Since GLib 2.64, calling this function after 374 * the initial handshake will no longer do anything. 375 * 376 * When using a #GTlsConnection created by #GSocketClient, the 377 * #GSocketClient performs the initial handshake, so calling this 378 * function manually is not recommended. 379 * 380 * #GTlsConnection::accept_certificate may be emitted during the 381 * handshake. 382 * 383 * Params: 384 * cancellable = a #GCancellable, or %NULL 385 * 386 * Returns: success or failure 387 * 388 * Since: 2.28 389 * 390 * Throws: GException on failure. 391 */ 392 public bool handshake(Cancellable cancellable) 393 { 394 GError* err = null; 395 396 auto __p = g_tls_connection_handshake(gTlsConnection, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 397 398 if (err !is null) 399 { 400 throw new GException( new ErrorG(err) ); 401 } 402 403 return __p; 404 } 405 406 /** 407 * Asynchronously performs a TLS handshake on @conn. See 408 * g_tls_connection_handshake() for more information. 409 * 410 * Params: 411 * ioPriority = the [I/O priority][io-priority] of the request 412 * cancellable = a #GCancellable, or %NULL 413 * callback = callback to call when the handshake is complete 414 * userData = the data to pass to the callback function 415 * 416 * Since: 2.28 417 */ 418 public void handshakeAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData) 419 { 420 g_tls_connection_handshake_async(gTlsConnection, ioPriority, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData); 421 } 422 423 /** 424 * Finish an asynchronous TLS handshake operation. See 425 * g_tls_connection_handshake() for more information. 426 * 427 * Params: 428 * result = a #GAsyncResult. 429 * 430 * Returns: %TRUE on success, %FALSE on failure, in which 431 * case @error will be set. 432 * 433 * Since: 2.28 434 * 435 * Throws: GException on failure. 436 */ 437 public bool handshakeFinish(AsyncResultIF result) 438 { 439 GError* err = null; 440 441 auto __p = g_tls_connection_handshake_finish(gTlsConnection, (result is null) ? null : result.getAsyncResultStruct(), &err) != 0; 442 443 if (err !is null) 444 { 445 throw new GException( new ErrorG(err) ); 446 } 447 448 return __p; 449 } 450 451 /** 452 * Sets the list of application-layer protocols to advertise that the 453 * caller is willing to speak on this connection. The 454 * Application-Layer Protocol Negotiation (ALPN) extension will be 455 * used to negotiate a compatible protocol with the peer; use 456 * g_tls_connection_get_negotiated_protocol() to find the negotiated 457 * protocol after the handshake. Specifying %NULL for the the value 458 * of @protocols will disable ALPN negotiation. 459 * 460 * See [IANA TLS ALPN Protocol IDs](https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids) 461 * for a list of registered protocol IDs. 462 * 463 * Params: 464 * protocols = a %NULL-terminated 465 * array of ALPN protocol names (eg, "http/1.1", "h2"), or %NULL 466 * 467 * Since: 2.60 468 */ 469 public void setAdvertisedProtocols(string[] protocols) 470 { 471 g_tls_connection_set_advertised_protocols(gTlsConnection, Str.toStringzArray(protocols)); 472 } 473 474 /** 475 * This sets the certificate that @conn will present to its peer 476 * during the TLS handshake. For a #GTlsServerConnection, it is 477 * mandatory to set this, and that will normally be done at construct 478 * time. 479 * 480 * For a #GTlsClientConnection, this is optional. If a handshake fails 481 * with %G_TLS_ERROR_CERTIFICATE_REQUIRED, that means that the server 482 * requires a certificate, and if you try connecting again, you should 483 * call this method first. You can call 484 * g_tls_client_connection_get_accepted_cas() on the failed connection 485 * to get a list of Certificate Authorities that the server will 486 * accept certificates from. 487 * 488 * (It is also possible that a server will allow the connection with 489 * or without a certificate; in that case, if you don't provide a 490 * certificate, you can tell that the server requested one by the fact 491 * that g_tls_client_connection_get_accepted_cas() will return 492 * non-%NULL.) 493 * 494 * Params: 495 * certificate = the certificate to use for @conn 496 * 497 * Since: 2.28 498 */ 499 public void setCertificate(TlsCertificate certificate) 500 { 501 g_tls_connection_set_certificate(gTlsConnection, (certificate is null) ? null : certificate.getTlsCertificateStruct()); 502 } 503 504 /** 505 * Sets the certificate database that is used to verify peer certificates. 506 * This is set to the default database by default. See 507 * g_tls_backend_get_default_database(). If set to %NULL, then 508 * peer certificate validation will always set the 509 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning 510 * #GTlsConnection::accept-certificate will always be emitted on 511 * client-side connections, unless that bit is not set in 512 * #GTlsClientConnection:validation-flags). 513 * 514 * There are nonintuitive security implications when using a non-default 515 * database. See #GDtlsConnection:database for details. 516 * 517 * Params: 518 * database = a #GTlsDatabase 519 * 520 * Since: 2.30 521 */ 522 public void setDatabase(TlsDatabase database) 523 { 524 g_tls_connection_set_database(gTlsConnection, (database is null) ? null : database.getTlsDatabaseStruct()); 525 } 526 527 /** 528 * Set the object that will be used to interact with the user. It will be used 529 * for things like prompting the user for passwords. 530 * 531 * The @interaction argument will normally be a derived subclass of 532 * #GTlsInteraction. %NULL can also be provided if no user interaction 533 * should occur for this connection. 534 * 535 * Params: 536 * interaction = an interaction object, or %NULL 537 * 538 * Since: 2.30 539 */ 540 public void setInteraction(TlsInteraction interaction) 541 { 542 g_tls_connection_set_interaction(gTlsConnection, (interaction is null) ? null : interaction.getTlsInteractionStruct()); 543 } 544 545 /** 546 * Since GLib 2.64, changing the rehandshake mode is no longer supported 547 * and will have no effect. With TLS 1.3, rehandshaking has been removed from 548 * the TLS protocol, replaced by separate post-handshake authentication and 549 * rekey operations. 550 * 551 * Deprecated: Changing the rehandshake mode is no longer 552 * required for compatibility. Also, rehandshaking has been removed 553 * from the TLS protocol in TLS 1.3. 554 * 555 * Params: 556 * mode = the rehandshaking mode 557 * 558 * Since: 2.28 559 */ 560 public void setRehandshakeMode(GTlsRehandshakeMode mode) 561 { 562 g_tls_connection_set_rehandshake_mode(gTlsConnection, mode); 563 } 564 565 /** 566 * Sets whether or not @conn expects a proper TLS close notification 567 * before the connection is closed. If this is %TRUE (the default), 568 * then @conn will expect to receive a TLS close notification from its 569 * peer before the connection is closed, and will return a 570 * %G_TLS_ERROR_EOF error if the connection is closed without proper 571 * notification (since this may indicate a network error, or 572 * man-in-the-middle attack). 573 * 574 * In some protocols, the application will know whether or not the 575 * connection was closed cleanly based on application-level data 576 * (because the application-level data includes a length field, or is 577 * somehow self-delimiting); in this case, the close notify is 578 * redundant and sometimes omitted. (TLS 1.1 explicitly allows this; 579 * in TLS 1.0 it is technically an error, but often done anyway.) You 580 * can use g_tls_connection_set_require_close_notify() to tell @conn 581 * to allow an "unannounced" connection close, in which case the close 582 * will show up as a 0-length read, as in a non-TLS 583 * #GSocketConnection, and it is up to the application to check that 584 * the data has been fully received. 585 * 586 * Note that this only affects the behavior when the peer closes the 587 * connection; when the application calls g_io_stream_close() itself 588 * on @conn, this will send a close notification regardless of the 589 * setting of this property. If you explicitly want to do an unclean 590 * close, you can close @conn's #GTlsConnection:base-io-stream rather 591 * than closing @conn itself, but note that this may only be done when no other 592 * operations are pending on @conn or the base I/O stream. 593 * 594 * Params: 595 * requireCloseNotify = whether or not to require close notification 596 * 597 * Since: 2.28 598 */ 599 public void setRequireCloseNotify(bool requireCloseNotify) 600 { 601 g_tls_connection_set_require_close_notify(gTlsConnection, requireCloseNotify); 602 } 603 604 /** 605 * Sets whether @conn uses the system certificate database to verify 606 * peer certificates. This is %TRUE by default. If set to %FALSE, then 607 * peer certificate validation will always set the 608 * %G_TLS_CERTIFICATE_UNKNOWN_CA error (meaning 609 * #GTlsConnection::accept-certificate will always be emitted on 610 * client-side connections, unless that bit is not set in 611 * #GTlsClientConnection:validation-flags). 612 * 613 * Deprecated: Use g_tls_connection_set_database() instead 614 * 615 * Params: 616 * useSystemCertdb = whether to use the system certificate database 617 */ 618 public void setUseSystemCertdb(bool useSystemCertdb) 619 { 620 g_tls_connection_set_use_system_certdb(gTlsConnection, useSystemCertdb); 621 } 622 623 /** 624 * Emitted during the TLS handshake after the peer certificate has 625 * been received. You can examine @peer_cert's certification path by 626 * calling g_tls_certificate_get_issuer() on it. 627 * 628 * For a client-side connection, @peer_cert is the server's 629 * certificate, and the signal will only be emitted if the 630 * certificate was not acceptable according to @conn's 631 * #GTlsClientConnection:validation_flags. If you would like the 632 * certificate to be accepted despite @errors, return %TRUE from the 633 * signal handler. Otherwise, if no handler accepts the certificate, 634 * the handshake will fail with %G_TLS_ERROR_BAD_CERTIFICATE. 635 * 636 * GLib guarantees that if certificate verification fails, this signal 637 * will be emitted with at least one error will be set in @errors, but 638 * it does not guarantee that all possible errors will be set. 639 * Accordingly, you may not safely decide to ignore any particular 640 * type of error. For example, it would be incorrect to ignore 641 * %G_TLS_CERTIFICATE_EXPIRED if you want to allow expired 642 * certificates, because this could potentially be the only error flag 643 * set even if other problems exist with the certificate. 644 * 645 * For a server-side connection, @peer_cert is the certificate 646 * presented by the client, if this was requested via the server's 647 * #GTlsServerConnection:authentication_mode. On the server side, 648 * the signal is always emitted when the client presents a 649 * certificate, and the certificate will only be accepted if a 650 * handler returns %TRUE. 651 * 652 * Note that if this signal is emitted as part of asynchronous I/O 653 * in the main thread, then you should not attempt to interact with 654 * the user before returning from the signal handler. If you want to 655 * let the user decide whether or not to accept the certificate, you 656 * would have to return %FALSE from the signal handler on the first 657 * attempt, and then after the connection attempt returns a 658 * %G_TLS_ERROR_BAD_CERTIFICATE, you can interact with the user, and 659 * if the user decides to accept the certificate, remember that fact, 660 * create a new connection, and return %TRUE from the signal handler 661 * the next time. 662 * 663 * If you are doing I/O in another thread, you do not 664 * need to worry about this, and can simply block in the signal 665 * handler until the UI thread returns an answer. 666 * 667 * Params: 668 * peerCert = the peer's #GTlsCertificate 669 * errors = the problems with @peer_cert. 670 * 671 * Returns: %TRUE to accept @peer_cert (which will also 672 * immediately end the signal emission). %FALSE to allow the signal 673 * emission to continue, which will cause the handshake to fail if 674 * no one else overrides it. 675 * 676 * Since: 2.28 677 */ 678 gulong addOnAcceptCertificate(bool delegate(TlsCertificate, GTlsCertificateFlags, TlsConnection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 679 { 680 return Signals.connect(this, "accept-certificate", dlg, connectFlags ^ ConnectFlags.SWAPPED); 681 } 682 }